package com.exposit.zbs.dao.impl;

import com.exposit.zbs.dao.IGrantAreaDAO;
import com.exposit.zbs.dao.base.BaseDAO;
import com.exposit.zbs.entity.GrantAreaEntity;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;


@Repository
@Transactional
public class GrantAreaDAO extends BaseDAO<GrantAreaEntity, Long> implements IGrantAreaDAO {

	@Override
	public GrantAreaEntity getGrantAreaByLink(String link) {

		GrantAreaEntity result = (GrantAreaEntity) this.getSession().createCriteria(GrantAreaEntity.class).add(Restrictions.eq("link", link)).uniqueResult();
		return result;
	}

	@Override
	public BigDecimal getAvailMoney(Long grantAreaId) {

		BigDecimal result = (BigDecimal) getSession().createSQLQuery("CALL GetAvailableMoneyForArea(:id)").setString("id", grantAreaId.toString()).uniqueResult();
		return result;
		/*Query("CALL GetAvailableMoneyForArea(:id)").setString("id", grantAreaId.toString()).uniqueResult();*/
	}

	@Override
	public BigDecimal getMoneySum(Long grantAreaId) {

		BigDecimal result = (BigDecimal) getSession().createSQLQuery("CALL GetSpendedMoneyForArea(:id)").setString("id", grantAreaId.toString()).uniqueResult();
		return result;
	}
}
